package com.jh.paymentchannel.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.jh.paymentchannel.pojo.enums.CmQueryPayStatus;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * 快银支付：查询支付订单结果
 */
@Entity
@Table(name = "t_congming_order_query")
public class CongMingOrderQuery implements Serializable {
    private static final long serialVersionUID = 7705248540411181078L;
    @Id
    @Column(name = "id")
    private long id;

    /**
     * 开店管家shopId
     */
    @Column(name = "shop_id")
    private String shopId;

    /**
     * 快银支付shopId
     */
    @Column(name = "cm_shop_id")
    private String cmShopId;

    /**
     * 开店管家订单号
     */
    @Column(name = "order_id")
    private String orderId;

    /**
     * 查询成功SUCCESS，查询失败FAIL
     */
    @Column(name = "result_code")
    private String resultCode;

    /**
     * 应付金额
     */
    @Column(name = "pay_able")
    private BigDecimal payable = BigDecimal.ZERO;

    /**
     * 优惠金额
     */
    @Column(name = "activity")
    private BigDecimal activity = BigDecimal.ZERO;

    /**
     * 交易金额，即实付金额
     */
    @Column(name = "money")
    private BigDecimal money = BigDecimal.ZERO;

    /**
     * 订单状态：0未支付成功，1支付成功，2支付中，3订单已关闭
     */
    @Column(name = "status")
    private long status = 1;

    /**
     * 交易发起时间：yyyy-MM-dd HH:mm:ss
     */
    @Column(name = "time")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date time;

    /**
     * 支付成功时间：yyyyMMddHHmmss
     */
    @Column(name = "time_end")
    private String timeEnd;

    /**
     * 快银订单号
     */
    @Column(name = "cm_order_id")
    private String cmOrderId;

    /**
     * 通道流水号，退款单号
     */
    @Column(name = "transaction_id")
    private String transactionId;

    /**
     * 手续费金额
     */
    @Column(name = "commission")
    private BigDecimal commission = BigDecimal.ZERO;

    /**
     * 支付类型：weixin微信支付，alipay支付宝支付
     */
    @Column(name = "order_type")
    private String orderType;

    /**
     * 退款状态：0未退款，1退款中，2退款成功
     */
    @Column(name = "refund_status")
    private long refundStatus = 0;

    /**
     * 退款金额
     */
    @Column(name = "refund_money")
    private BigDecimal refundMoney = BigDecimal.ZERO;

    /**
     * 最后一笔退款时间：yyyy-MM-dd HH:mm:ss
     */
    @Column(name = "refund_time")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date refundTime;

    /**
     * 退款详情：退款状态为1或2时存在
     */
    @Column(name = "refund_info")
    private String refundInfo;

    @Column(name = "create_time")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getShopId() {
        return shopId;
    }

    public void setShopId(String shopId) {
        this.shopId = shopId;
    }

    public String getCmShopId() {
        return cmShopId;
    }

    public void setCmShopId(String cmShopId) {
        this.cmShopId = cmShopId;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public BigDecimal getPayable() {
        return payable;
    }

    public void setPayable(BigDecimal payable) {
        this.payable = payable;
    }

    public BigDecimal getActivity() {
        return activity;
    }

    public void setActivity(BigDecimal activity) {
        this.activity = activity;
    }

    public BigDecimal getMoney() {
        return money;
    }

    public void setMoney(BigDecimal money) {
        this.money = money;
    }

    public long getStatus() {
        return status;
    }

    public void setStatus(long status) {
        this.status = status;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public String getTimeEnd() {
        return timeEnd;
    }

    public void setTimeEnd(String timeEnd) {
        this.timeEnd = timeEnd;
    }

    public String getCmOrderId() {
        return cmOrderId;
    }

    public void setCmOrderId(String cmOrderId) {
        this.cmOrderId = cmOrderId;
    }

    public String getTransactionId() {
        return transactionId;
    }

    public void setTransactionId(String transactionId) {
        this.transactionId = transactionId;
    }

    public BigDecimal getCommission() {
        return commission;
    }

    public void setCommission(BigDecimal commission) {
        this.commission = commission;
    }

    public String getOrderType() {
        return orderType;
    }

    public void setOrderType(String orderType) {
        this.orderType = orderType;
    }

    public long getRefundStatus() {
        return refundStatus;
    }

    public void setRefundStatus(long refundStatus) {
        this.refundStatus = refundStatus;
    }

    public BigDecimal getRefundMoney() {
        return refundMoney;
    }

    public void setRefundMoney(BigDecimal refundMoney) {
        this.refundMoney = refundMoney;
    }

    public Date getRefundTime() {
        return refundTime;
    }

    public void setRefundTime(Date refundTime) {
        this.refundTime = refundTime;
    }

    public String getRefundInfo() {
        return refundInfo;
    }

    public void setRefundInfo(String refundInfo) {
        this.refundInfo = refundInfo;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

//    public String getStatusOf() {
//        return CmQueryPayStatus.of(getStatus());
//    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CongMingOrderQuery)) return false;
        CongMingOrderQuery that = (CongMingOrderQuery) o;
        return getStatus() == that.getStatus() &&
                getRefundStatus() == that.getRefundStatus() &&
                Objects.equals(getId(), that.getId()) &&
                Objects.equals(getShopId(), that.getShopId()) &&
                Objects.equals(getCmShopId(), that.getCmShopId()) &&
                Objects.equals(getOrderId(), that.getOrderId()) &&
                Objects.equals(getResultCode(), that.getResultCode()) &&
                Objects.equals(getPayable(), that.getPayable()) &&
                Objects.equals(getActivity(), that.getActivity()) &&
                Objects.equals(getMoney(), that.getMoney()) &&
                Objects.equals(getTime(), that.getTime()) &&
                Objects.equals(getTimeEnd(), that.getTimeEnd()) &&
                Objects.equals(getCmOrderId(), that.getCmOrderId()) &&
                Objects.equals(getTransactionId(), that.getTransactionId()) &&
                Objects.equals(getCommission(), that.getCommission()) &&
                Objects.equals(getOrderType(), that.getOrderType()) &&
                Objects.equals(getRefundMoney(), that.getRefundMoney()) &&
                Objects.equals(getRefundTime(), that.getRefundTime()) &&
                Objects.equals(getRefundInfo(), that.getRefundInfo()) &&
                Objects.equals(getCreateTime(), that.getCreateTime());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId(), getShopId(), getCmShopId(), getOrderId(), getResultCode(), getPayable(), getActivity(), getMoney(), getStatus(), getTime(), getTimeEnd(), getCmOrderId(), getTransactionId(), getCommission(), getOrderType(), getRefundStatus(), getRefundMoney(), getRefundTime(), getRefundInfo(), getCreateTime());
    }

    @Override
    public String toString() {
        return "CongMingOrderQuery{" +
                "id='" + id + '\'' +
                ", shopId='" + shopId + '\'' +
                ", cmShopId='" + cmShopId + '\'' +
                ", orderId='" + orderId + '\'' +
                ", resultCode='" + resultCode + '\'' +
                ", payable=" + payable +
                ", activity=" + activity +
                ", money=" + money +
                ", status=" + status +
                ", time=" + time +
                ", timeEnd='" + timeEnd + '\'' +
                ", cmOrderId='" + cmOrderId + '\'' +
                ", transactionId='" + transactionId + '\'' +
                ", commission='" + commission + '\'' +
                ", orderType='" + orderType + '\'' +
                ", refundStatus=" + refundStatus +
                ", refundMoney=" + refundMoney +
                ", refundTime=" + refundTime +
                ", refundInfo='" + refundInfo + '\'' +
                ", createTime=" + createTime +
                '}';
    }

    public void addCongMingOrderQuery(CmOrderQuery cmOrderQuery, String shopId) throws ParseException {
        setActivity(cmOrderQuery.getActivity());
        setCmShopId(cmOrderQuery.getShopId());
        setCommission(cmOrderQuery.getCommission());
        setMoney(cmOrderQuery.getMoney());
        setOrderType(cmOrderQuery.getOrder_type());
        setOrderId(cmOrderQuery.getOrderId());
        setRefundMoney(cmOrderQuery.getRefund_money());
        setRefundStatus(cmOrderQuery.getRefundStatus());
        setCmOrderId(cmOrderQuery.getCmOrderId());
        setResultCode(cmOrderQuery.getResult_code());
        setPayable(cmOrderQuery.getPayable());
        setShopId(shopId);
        setStatus(cmOrderQuery.getStatus());
        setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(cmOrderQuery.getTime()));
        setTimeEnd(cmOrderQuery.getTimeEnd());
    }
}
